package org.openrbac.rbac.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.openrbac.rbac.entity.Auth;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;

/**
* @author bapie
* @description 针对表【auth(系统菜单)】的数据库操作Mapper
* @createDate 2024-04-03 15:37:15
* @Entity org.openrbac.entity.Auth
*/
public interface AuthMapper extends BaseMapper<Auth> {

    @Select("select * from auth where name=#{name}")
    Auth selectAuthByName(@Param("name") String name);

    @Select("<script>" +
            "select auth.id,auth.pid,auth.name,auth.path,auth.redirect,auth.component,auth.meta_title,auth.meta_icon " +
            "from role_auth left join auth on role_auth.auth_id=auth.id where role_auth.role_id in " +
            "<foreach collection='roleIds' item='item' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    List<Auth> listAuthByRoleIds(@Param("roleIds") List<Long> roleIds);

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Select("select * from auth where pid=#{pid}")
    Auth selectAuthById(@Param("id") int id);

    @Delete("<script>"+
            "delete from role_auth where auth_id in " +
            "<foreach item='item' collection='authIds' open='(' separator=',' close=')'>"+
            "#{item}"+
            "</foreach>"+
            "</script>")
    int delRoleAuthByAuthIds(@Param("authIds") List<Long> authIds);

    @Update("<script>"+
            "update auth set is_deleted = 1 "+
            "where id in "+
            "<foreach item='item' collection='authIds' open='(' separator=',' close=')'>"+
            "#{item}"+
            "</foreach>"+
            "</script>"
    )
    int delAuthByIds(@Param("authIds") List<Long> authIds);
}




